using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using BarCodeDB.Entities;
using BarCodeDB.Services;
using Microsoft.Practices.EnterpriseLibrary.Logging;

/// <summary>
/// 
/// </summary>
public partial class Admin_ReviewFormXML : System.Web.UI.Page
{
    private string _DeCryptedPwd;
    private string _UserName;
    private string _UserDomain;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        XmlDocument _XmlDoc = new XmlDocument();
        string _Path = "";

        SFG.Common.Utilities.Encryptor _Encryptor = new SFG.Common.Utilities.Encryptor(SFG.Common.Utilities.Encryptor.SymmProvEnum.Rijndael);
        _DeCryptedPwd = _Encryptor.Decrypting(WebConfigurationManager.AppSettings["UserPwd"], WebConfigurationManager.AppSettings["EncryptKey"]);

        _UserName = WebConfigurationManager.AppSettings["UserAcct"];
        _UserDomain = WebConfigurationManager.AppSettings["UserDomain"];

        _Path = WebConfigurationManager.AppSettings["FileDrop"];

        if (!IsPostBack)
        {
            using (new SFG.Common.Utilities.Impersonator(_UserName, _UserDomain, _DeCryptedPwd))
            {
                String[] _FileList = Directory.GetFiles(_Path, "*.xml");

                this.lstFiles.DataSource = _FileList;
                this.lstFiles.DataBind();

                _XmlDoc.Load(_Path + "/TargetData.xml");
                this.FreeTextBox1.Text = _XmlDoc.InnerXml;
            }
            _XmlDoc = null;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
    {
        string _Path = lstFiles.SelectedItem.Text;
        BarcodeProcessor _AppUtil = new BarcodeProcessor();

        XmlDocument _XmlDoc = new XmlDocument();
        try
        {
            using (new SFG.Common.Utilities.Impersonator(_UserName, _UserDomain, _DeCryptedPwd))
            {
                _XmlDoc.Load(_Path);

                XmlElement encriptedElement = (XmlElement)_XmlDoc.GetElementsByTagName("EncryptedData")[0];

                if (encriptedElement != null)
                {
                    AppUtil.DecriptXmlDoc(ref _AppUtil, ref _XmlDoc, ref encriptedElement);

                    //Display Data
                    this.FreeTextBox1.Text = _XmlDoc.OuterXml;

                    _AppUtil.XMLDoc = _XmlDoc;
                    this.txtFormName.Text = _AppUtil.GetFormName();
                    AppUtil.LogError(this.txtFormName.Text);
                    Guid _Guid = _AppUtil.GetFormID();
                    this.txtFormID.Text = _Guid.ToString();
                }
                else
                {
                    this.FreeTextBox1.Text = "Password not specified!";
                }
            }
        }
        catch (System.Xml.XmlException xe)
        {
            AppUtil.LogError(xe.Message);
            this.FreeTextBox1.Text = xe.Message;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="_AppUtil"></param>
    /// <param name="_XmlDoc"></param>
    /// <param name="encriptedElement"></param>
    //private void DecriptXmlDoc(ref BarcodeProcessor _AppUtil, ref XmlDocument _XmlDoc, ref XmlElement encriptedElement)
    //{
    //    //Decript        
    //    TripleDESCryptoServiceProvider tDESKey = new TripleDESCryptoServiceProvider();
    //    byte[] _Key = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };
    //    tDESKey.Key = _Key;

    //    EncryptedData encryptedData = new EncryptedData();

    //    encryptedData.Type = EncryptedXml.XmlEncElementUrl;
    //    encryptedData.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncTripleDESUrl);

    //    encryptedData.LoadXml(encriptedElement);

    //    EncryptedXml exml = new EncryptedXml();

    //    Byte[] encryptedForm = exml.DecryptData(encryptedData, tDESKey);

    //    exml.ReplaceData(encriptedElement, encryptedForm);

    //    ////Display Data
    //    //this.FreeTextBox1.Text = _XmlDoc.OuterXml;

    //    //_AppUtil.XMLDoc = _XmlDoc;
    //    //this.txtFormName.Text = _AppUtil.GetFormName();
    //    //Guid _Guid = _AppUtil.GetFormID();
    //    //this.txtFormID.Text = _Guid.ToString();
    //}

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnComplete_Click(object sender, EventArgs e)
    {
        BarCodeDB.Services.FormsService _FormSvc = new FormsService();
        BarCodeDB.Entities.Forms _Form = _FormSvc.GetByFormId(new Guid(this.txtFormID.Text));

        if (_Form == null)
        {
            _Form = new BarCodeDB.Entities.Forms();

            _Form.FormId = new Guid(this.txtFormID.Text);
            _Form.FormName = this.txtFormName.Text;
            _FormSvc.Insert(_Form);

            _FormSvc = null;
            _Form.Dispose();
        }

        BarCodeDB.Services.FormsWorkTypeService _FormWrktSvc = new FormsWorkTypeService();
        BarCodeDB.Entities.FormsWorkType _FormWrkt = _FormWrktSvc.GetByFormId(new Guid(this.txtFormID.Text));

        if (_FormWrkt == null)
        {
            _FormWrkt = new BarCodeDB.Entities.FormsWorkType();
            _FormWrkt.FormId = new Guid(this.txtFormID.Text);
            _FormWrkt.WorkType = this.txtWorkType.Text;

            _FormWrktSvc.Insert(_FormWrkt);
            _FormWrktSvc = null;
            _FormWrkt.Dispose();
        }

        this.txtFormID.Text = "";
        this.txtWorkType.Text = "";
        this.txtFormName.Text = "done.";
    }
}
